<?php


namespace app\api\model;


use think\Model;

class GlobalConfig extends Model
{
    /**
     * get All
     * @return array
     */
    public static function getAllConfig(): array
    {
        $config = self::field('k,v')->select();
        $global_config = [];
        foreach ($config as $k => $v) {
            $global_config[$v['k']] = $v['v'];
        }
        return $global_config;
    }


    /**
     * @param $type
     * @return array
     */
    public static function getValues($type): array
    {
        $config = self::select();
        $newConfig = array();
        foreach ($config as $v) {
            $newConfig[$v['k']] = $v['v'];
        }
        $re = array();
        //如果是数组
        if (is_array($type)) {
            foreach ($type as $v) {
                $re[$v] = !empty($newConfig[$v]) ? $newConfig[$v] : '';
            }
        } else {
            $re[$type] = !empty($newConfig[$type]) ? $newConfig[$type] : "";
        }
        return $re;
    }

    /**
     * @param $key
     * @param $value
     * @return GlobalConfig
     */
    public static function updateValue($key, $value): GlobalConfig
    {
        return self::where(['k' => $key])->update(['v' => $value]);
    }

    public static function updateExampleSign($sign, $redis)
    {
        $ret = self::where(['k' => 'example_sign'])->update(['v' => $sign]);
        $redis->del('drill_global_configce', 'config');
        return $ret;
    }

    /**
     * @param string|array $type
     * @return array
     */
    public function getconfig($type)
    {
        $config = self::getAllConfig();
        $re = array();
        //如果是数组
        if (is_array($type)) {
            foreach ($type as $v) {
                $re[$v] = !empty($config[$v]) ? $config[$v] : '';
            }
        } else {
            $re[$type] = !empty($config[$type]) ? $config[$type] : "";
        }
        return $re;
    }
}